package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patchmanage.back.entity.TaskInfo;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.ITaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-07-11
 */
@Api(value = " 任务列表 controller", tags = {" 任务列表 Api"})
@RestController
@RequestMapping("/back/taskinfo")
public class TaskInfoController {

    @Autowired
    private ITaskInfoService taskInfoService;

    /**
     * 获取任务列表
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取任务列表接口", notes = "获取任务列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "taskType", value = "taskType", required = false, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "Integer", paramType = "body")
    })
    public Result<IPage<TaskInfo>> list(@ApiIgnore @RequestBody Dict dict) {

        Integer pageNo = dict.getInt("pageNo");
        Integer pageSize = dict.getInt("pageSize");
        Integer taskType = dict.getInt("taskType");
        String name = dict.getStr("name");

        Page<TaskInfo> page = new Page(pageNo, pageSize);
        QueryWrapper<TaskInfo> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if (taskType != null) {
            queryWrapper.eq("task_type", taskType);
        }
        return Result.success(taskInfoService.page(page, queryWrapper));
    }
}